(ns lastfm_music_recommendation.search.search
  (:require [lastfm-music-recommendation.db.datomic :as datomic :only [find-by-keyword]])
  (:require [lastfm-music-recommendation.search.nnet :as nnet])
  (:require [clojure.string :as s]))

(defn db-search
  [query]
  (reduce (fn [result keyword] (reduce (fn [r song] (if (r song)
                                                      (assoc r song (inc (r song)))
                                                      (assoc r song 1)))
                                       result (datomic/find-by-keyword keyword)))
          {} (s/split query #" ")))

(defn search
  [query]
  (let [db-result (db-search query)]
    (if-not (empty? db-result)
      (let [songs (map #(ffirst %) db-result)
            scores (nnet/get-result query songs)]
        (reverse (sort-by val (into {} (map #(conj [] (%1 0) (* (%1 1) %2)) 
                                            db-result scores))))))))







